package me.jessyan.mvparms.demo.mvp.ui.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.platform.ai.control.R;


/**
 * The author: Zhao X.H.
 * Creation time: 2018/2/14 ${time}
 */

public class SpinnerPopWindow<T extends String> extends BackgroundDarkPopupWindow implements AdapterView.OnItemClickListener {

    private ListView mListView;
    private Context mContext;
    private MItemSelectListener mItemSelectListener;
    private ArrayAdapter<T> myAdapter;

    public SpinnerPopWindow(Context context,View mContent,MItemSelectListener mItemSelectListener) {
        super(mContent, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT);
        this.mItemSelectListener = mItemSelectListener;
        this.mContext = context;
        init();
    }

    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.popwindow_spinner, null);
        setContentView(view);
        mListView = (ListView) view.findViewById(R.id.spinner_list);
        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        setFocusable(true);//设置为true的时候才能点击列表中的item
        ColorDrawable cd = new ColorDrawable(Color.TRANSPARENT);
//        setAnimationStyle(R.style.Popup_Spinner);
//        ColorDrawable dw = new ColorDrawable(-00000);// 半透明
        setBackgroundDrawable(cd);// 设置背景图片，不能在布局中设置，要通过代码来设置
        setOutsideTouchable(true);// 触摸popupwindow外部，popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功，如上
//        setBackgroundAlpha(0.5f);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mItemSelectListener.onItemClick(position);
            }
        });
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
//                setBackgroundAlpha(1.0f);
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (mItemSelectListener != null) {
            mItemSelectListener.onItemClick(i);
        }
        dismiss();
    }

    //设置适配器
    public void setAdapter(ArrayAdapter<T> myAdapter) {
        this.myAdapter = myAdapter;
        this.mListView.setAdapter(this.myAdapter);
    }

    //更新列表
    public void setNotify() {
        this.myAdapter.notifyDataSetChanged();
    }

    public void setDismissAction(ImageView img){
    }

    //设置监听
    public void setItemSelectListener(MItemSelectListener mItemSelectListener) {
        this.mItemSelectListener = mItemSelectListener;
    }

    //自定义list中item的点击监听
    public interface MItemSelectListener {
        void onItemClick(int position);
    }

    public void showPopupWindow(View v){
       showAsDropDown(v);//在v的下面
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     *            屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) mContext).getWindow().setAttributes(lp);

    }
}
